package com.project.webservice.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.util.JSONUtils;
import com.project.webservice.dto.GetTidLivDto;
import com.project.webservice.entity.DmTidStatus;
import com.project.webservice.entity.TbRfidSysConditionTidUid;
import com.project.webservice.entity.TbRfidSysConditionTidXuhao;
import com.project.webservice.mapper.DmTidStatusMapper;
import com.project.webservice.service.DmTidStatusService;
import com.project.webservice.service.TbRfidSysConditionTidUidService;
import com.project.webservice.service.TbRfidSysConditionTidXuhaoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
@DS("rfidrun")
public class DmTidStatusServiceImpl extends ServiceImpl<DmTidStatusMapper, DmTidStatus> implements DmTidStatusService {

    @Resource
    private TbRfidSysConditionTidUidService tbRfidSysConditionTidUidService;
    @Resource
    private TbRfidSysConditionTidXuhaoService tbRfidSysConditionTidXuhaoService;
    /**
     * 电子标签运转情况 getTIDLiv
     * @param page
     * @param search
     * @return
     */
    public IPage<GetTidLivDto> getTidLiv(Page page, GetTidLivDto search){
        log.info("getTbTidRunDayByPage:{}", JSONUtils.writeExcludeNull(search));
        if(StringUtils.isNotBlank(search.getTidUid())){
            //先插入TB_RFID_SYS_CONDITION_TID_UID
            String[] tidUids = StringUtils.split(search.getTidUid(),",");
            String conditionId = UUID.randomUUID().toString();
            List<TbRfidSysConditionTidUid> tbRfidSysConditionTidUids = new ArrayList<>();
            for(String tidUid:tidUids){
                tbRfidSysConditionTidUids.add(new TbRfidSysConditionTidUid(conditionId,tidUid));
            }
            // 调用单独的方法保存数据到gjbuscondition数据源
            boolean isSave = tbRfidSysConditionTidUidService.saveBatch(tbRfidSysConditionTidUids);
            log.info("保存TID_UID条件数据结果: {}, conditionId: {}", isSave, conditionId);
            search.setConditionId(conditionId);
        }else if(StringUtils.isNotBlank(search.getTidxuhao())){
            String[] tidxuhaos = StringUtils.split(search.getTidxuhao(),",");
            String conditionId = UUID.randomUUID().toString();
            List<TbRfidSysConditionTidXuhao> tbRfidSysConditionTidXuhaos = new ArrayList<>();
            for(String tidxuhao:tidxuhaos){
                tbRfidSysConditionTidXuhaos.add(new TbRfidSysConditionTidXuhao(conditionId,tidxuhao));
            }
            // 调用单独的方法保存数据到gjbuscondition数据源
            boolean isSave = tbRfidSysConditionTidXuhaoService.saveBatch(tbRfidSysConditionTidXuhaos);
            search.setConditionSn(conditionId);
        }
        return baseMapper.getTidLiv(page,search);

    }

}
